<?php
/**
 * Created by jinqiang on 2024/11/18 13:41.
 * Copyright (c) 2024, Your Company. All rights reserved.
 */


namespace App\Http\Controllers\Callback;


use App\Http\Controllers\Controller;
use App\Http\Repository\FeishuRepository;
use App\Http\Services\Callback\FeiShuService;
use Illuminate\Http\Request;

class FeiShuController extends Controller
{
    //快哉
    public function kz(Request $request, FeiShuService $service)
    {
        $this->validate($request, [
            'encrypt' => 'required'
        ]);
        $param = $request->all();
        $data = FeishuRepository::decryptData($param['encrypt'], "kz");//消息解密
        //注册回调地址
        if($data['challenge'] ?? "" && $data['type'] ?? "" && $data['type'] == 'url_verification'){
            info("快哉飞书 注册回调接口: ", ["密文" => $param['encrypt'], "明文"=>$data]);
            echo json_encode($data, JSON_UNESCAPED_UNICODE); exit;
        }
        $res = $service->getMessage($data, 'kz');
        if($res['code'] !== 0){
            info("快哉飞书回调(ERROR):", ["密文" => $param['encrypt'], "明文"=>$data, "error" => $res]);
        }else{
            info("快哉飞书回调:", ["密文" => $param['encrypt'], "明文"=>$data]);
        }
        return $res['code'] === 0 ? success() : error($res['msg'], $res);
    }

    //妙哉
    public function mz(Request $request, FeiShuService $service)
    {
        $this->validate($request, [
            'encrypt' => 'required'
        ]);
        $param = $request->all();
        $data = FeishuRepository::decryptData($param['encrypt'], "mz");//消息解密
        //注册回调地址
        if($data['challenge'] ?? "" && $data['type'] ?? "" && $data['type'] == 'url_verification'){
            info("妙哉飞书 注册回调接口: ", ["密文" => $param['encrypt'], "明文"=>$data]);
            echo json_encode($data, JSON_UNESCAPED_UNICODE); exit;
        }
        $res = $service->getMessage($data, 'mz');
        if($res['code'] !== 0){
            info("妙哉飞书回调(ERROR):", ["密文" => $param['encrypt'], "明文"=>$data, "error" => $res]);
        }else{
            info("妙哉飞书回调:", ["密文" => $param['encrypt'], "明文"=>$data]);
        }
        return $res['code'] === 0 ? success() : error($res['msg']);
    }

    //有香
    public function yx(Request $request, FeiShuService $service)
    {
        $this->validate($request, [
            'encrypt' => 'required'
        ]);
        $param = $request->all();
        $data = FeishuRepository::decryptData($param['encrypt'], "yx");//消息解密
        //注册回调地址
        if($data['challenge'] ?? "" && $data['type'] ?? "" && $data['type'] == 'url_verification'){
            info("有香飞书 注册回调接口: ", ["密文" => $param['encrypt'], "明文"=>$data]);
            echo json_encode($data, JSON_UNESCAPED_UNICODE); exit;
        }
        $res = $service->getMessage($data, 'yx');
        if($res['code'] !== 0){
            info("有香飞书回调(ERROR):", ["密文" => $param['encrypt'], "明文"=>$data, "error" => $res]);
        }else{
            info("有香飞书回调:", ["密文" => $param['encrypt'], "明文"=>$data]);
        }
        return $res['code'] === 0 ? success() : error($res['msg']);
    }

    //弦木
    public function xm(Request $request, FeiShuService $service)
    {
        $this->validate($request, [
            'encrypt' => 'required'
        ]);
        $param = $request->all();
        $data = FeishuRepository::decryptData($param['encrypt'], "xm");//消息解密
        //注册回调地址
        if($data['challenge'] ?? "" && $data['type'] ?? "" && $data['type'] == 'url_verification'){
            info("弦木飞书 注册回调接口: ", ["密文" => $param['encrypt'], "明文"=>$data]);
            echo json_encode($data, JSON_UNESCAPED_UNICODE); exit;
        }
        $res = $service->getMessage($data, 'xm');
        if($res['code'] !== 0){
            info("弦木飞书回调(ERROR):", ["密文" => $param['encrypt'], "明文"=>$data, "error" => $res]);
        }else{
            info("弦木飞书回调:", ["密文" => $param['encrypt'], "明文"=>$data]);
        }
        return $res['code'] === 0 ? success() : error($res['msg']);
    }

    //锦瑟
    public function js(Request $request, FeiShuService $service)
    {
        $this->validate($request, [
            'encrypt' => 'required'
        ]);
        $param = $request->all();
        $data = FeishuRepository::decryptData($param['encrypt'], "js");//消息解密
        //注册回调地址
        if($data['challenge'] ?? "" && $data['type'] ?? "" && $data['type'] == 'url_verification'){
            info("锦瑟飞书 注册回调接口: ", ["密文" => $param['encrypt'], "明文"=>$data]);
            echo json_encode($data, JSON_UNESCAPED_UNICODE); exit;
        }
        $res = $service->getMessage($data, 'js');
        if($res['code'] !== 0){
            info("锦瑟飞书回调(ERROR):", ["密文" => $param['encrypt'], "明文"=>$data, "error" => $res]);
        }else{
            info("锦瑟飞书回调:", ["密文" => $param['encrypt'], "明文"=>$data]);
        }
        return $res['code'] === 0 ? success() : error($res['msg']);
    }

    //七月夕
    public function qyx(Request $request, FeiShuService $service)
    {
        $this->validate($request, [
            'encrypt' => 'required'
        ]);
        $param = $request->all();
        $data = FeishuRepository::decryptData($param['encrypt'], "qyx");//消息解密
        //注册回调地址
        if($data['challenge'] ?? "" && $data['type'] ?? "" && $data['type'] == 'url_verification'){
            info("七月夕飞书 注册回调接口: ", ["密文" => $param['encrypt'], "明文"=>$data]);
            echo json_encode($data, JSON_UNESCAPED_UNICODE); exit;
        }
        $res = $service->getMessage($data, 'qyx');
        if($res['code'] !== 0){
            info("七月夕飞书回调(ERROR):", ["密文" => $param['encrypt'], "明文"=>$data, "error" => $res]);
        }else{
            info("七月夕飞书回调:", ["密文" => $param['encrypt'], "明文"=>$data]);
        }
        return $res['code'] === 0 ? success() : error($res['msg']);
    }
}
